package com.ca.ui.core.view.card;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ca.ui.core.R;
import com.ca.ui.core.util.ImageUtils;
import com.ca.ui.core.util.NumberUtils;
import com.ca.ui.core.view.list.MiniHeaderImageListView;

public class BigInfoCard extends ConstraintLayout {

    private String imageUrl = "";
    private String name = "";
    private int paperCount = 0;
    private int followCount = 0;
    private String[] headerImageArray;

    public ImageView imageView;
    public TextView nameView;
    public TextView paperCountView;
    public MiniHeaderImageListView headerImageList;
    public TextView followCountView;
    public ImageView rightView;
    public Button followButton;

    public BigInfoCard(@NonNull Context context) {
        super(context);
        initView(context, null);
    }

    public BigInfoCard(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public BigInfoCard(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {

        View view = LayoutInflater.from(context).inflate(R.layout.big_info_card_layout, this);
        imageView = view.findViewById(R.id.iv);
        nameView = view.findViewById(R.id.tv_name);
        paperCountView = view.findViewById(R.id.tv_paperCount);
        headerImageList = view.findViewById(R.id.list_miniHeaderImage);
        followCountView = view.findViewById(R.id.tv_followCount);
        rightView = view.findViewById(R.id.iv_right);
        followButton = view.findViewById(R.id.btn_follow);

        followButton.setBackgroundColor(getResources().getColor(R.color.primary));

        if (attrs != null) {
            TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.BigInfoCard, 0, 0);

            imageUrl = array.getString(R.styleable.BigInfoCard_bic_imageUrl);
            name = array.getString(R.styleable.BigInfoCard_bic_name);
            paperCount = array.getInteger(R.styleable.BigInfoCard_bic_paperCount, 0);
            followCount = array.getInteger(R.styleable.BigInfoCard_bic_followCount, 0);

            array.recycle();
        }

        setImageUrl(imageUrl);
        setName(name);
        setPaperCount(paperCount);
        setFollowCount(followCount);
        setHeaderImageArray(headerImageArray);
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl == null ? "" : imageUrl;
        ImageUtils.loadRoundImage(getContext(), this.imageUrl, this.imageView, 20);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? "" : name;
        this.nameView.setText(this.name);
    }

    public int getPaperCount() {
        return paperCount;
    }

    public void setPaperCount(int paperCount) {
        this.paperCount = paperCount;
        String text = NumberUtils.numberToString((long) this.paperCount);
        text += "讨论";
        this.paperCountView.setText(text);
    }

    public int getFollowCount() {
        return followCount;
    }

    public void setFollowCount(int followCount) {
        this.followCount = followCount;
        String text = NumberUtils.numberToString((long) this.followCount);
        text += "关注";
        this.followCountView.setText(text);

        if (this.followCount == 0) {
            this.followCountView.setVisibility(GONE);
            this.rightView.setVisibility(GONE);
            this.headerImageList.setVisibility(GONE);
        }
        else {
            this.followCountView.setVisibility(VISIBLE);
            this.rightView.setVisibility(VISIBLE);
            this.headerImageList.setVisibility(VISIBLE);
        }
    }

    public String[] getHeaderImageArray() {
        return headerImageArray;
    }

    public void setHeaderImageArray(String[] headerImageArray) {
        this.headerImageArray = headerImageArray == null ? new String[0] : headerImageArray;
        this.headerImageList.setImageUrlArray(this.headerImageArray);
        if (this.headerImageArray.length == 0) {
            this.headerImageList.setVisibility(GONE);
        }
        else {
            this.headerImageList.setVisibility(VISIBLE);
        }
    }
}
